<?php
namespace app\admin\controller;
use \think\Controller;
use \think\Db;
use \think\Request;
use \think\facade\Session;

/*后台登陆入口*/
class Index extends Base
{   
    /*登录页面及登陆验证*/
    public function login(Request $request)
    {   
    	$data=$request->post();
    	if(empty($data)){
          return $this->fetch();
    	}else{
          if(empty($data['username'])){
            $result=['status'=>'0','msg'=>'请输入用户名'];
            return json($result);
          }
          if(empty($data['password'])){
           	$result=['status'=>'0','msg'=>'请输入密码'];
           	return json($result);
          }

          $model=new \app\admin\model\Admin();/*调用模型查询登陆数据*/
          $model_data=$model->get_data($data['username']);
         
          if(empty($model_data)){
            $result=['status'=>'0','msg'=>'不存在的账户'];
           	return json($result);
          }else{
            if(md5(trim($data['password']))!=$model_data['password']){
            	$result=['status'=>'0','msg'=>'密码错误'];
           	  return json($result);
            }else{ //登陆成功
              //存入session 跳转到后台
              Session::set('username',trim($data['username']));
              $result=['status'=>'1','msg'=>'登录成功'];
              return json($result); 
            }
          }
      }
    }

    /*首页*/
    public function home(Request $request){
      $this->check_session();
      /*左侧导航*/
      $data_get=$request->get()['nav'];
      $data=$this->navleft($data_get);
      $this->assign($data);

     return $this->fetch();
    }

    /*退出*/
    public function out(){
       Session::clear();
       $result=['status'=>'1','msg'=>'退出成功'];
       return json($result);
    }

    
    /*修改密码*/
    public function EtidPwd(Request $request){
       if(Session::has('username')==1){
          $data=$request->post();
          $username=Session::get('username');
          if(empty($data['oldPassword'])){
             $result=['status'=>'0','msg'=>'请输入原密码'];
             return json($result);
          }
          if(empty($data['password'])){
             $result=['status'=>'0','msg'=>'请输入新密码'];
             return json($result);
          }
          if(empty($data['repassword'])){
             $result=['status'=>'0','msg'=>'请输入确认密码'];
             return json($result);
          }
          if(trim($data['repassword'])!=trim($data['password'])){
             $result=['status'=>'0','msg'=>'两次密码不一致'];
             return json($result);
          }
          $model=new \app\admin\model\Admin();/*调用模型查询登陆数据*/
          $model_data=$model->get_data($username);
          if($model_data['password']!=md5(trim($data['oldPassword']))){
            $result=['status'=>'0','msg'=>'原密码不正确'];
            return json($result);
          }else{
            $model_update=$model->update_data($username,md5(trim($data['password'])));
            $result=['status'=>'1','msg'=>'更改成功'];
            return json($result);
          }
       }else{
          $result=['status'=>'0','msg'=>'登陆已过期,请重新登陆'];
          return json($result);
       }
       
    }

    public function user(Request $request){
        $user=Db::connect('publicdata')->table('lily_admin')->select();
        $this->check_session();
        $auth_data=array(
            ',1,'=>'账户列表',
            ',2,'=>'博客/博客分类',
            ',3,'=>'新闻事件/新闻事件分类',
            ',4,'=>'其他',
            ',4.1,'=>'其他/询问表单',
            ',4.2,'=>'其他/订阅邮箱',
            ',4.3,'=>'其他/搜索信息',
            ',5,'=>'产品',
            ',6,'=>'301',
        );
        /*左侧导航*/
        $data_get=$request->get()['nav'];
        $data=$this->navleft($data_get);
        $data['user']=$user;
        $data['auth_data']=$auth_data;
        $this->assign($data);
        return $this->fetch();
    }

}
